package com.hsylient.event.controller;

import com.hsylient.event.entity.Order;
import com.hsylient.event.event.PriceLogEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;

@RestController
public class OrderController {

    @Autowired
    private ApplicationContext applicationContext;


    @GetMapping("/book/order")
    public Order order() {
        Order order = Order.builder()
                .id(1001)
                .count(10)
                .name("大宝SOD密")
                .userId(10001L)
                .price(BigDecimal.TEN)
                .createTime(new Date())
                .updateTime(new Date())
                .build();
        applicationContext.publishEvent(order);
        return order;
    }


    @GetMapping("/price/log")
    public Order priceLog() {
        Order order = Order.builder()
                .id(1001)
                .count(10)
                .name("谁能够代替你呀")
                .userId(10001L)
                .price(BigDecimal.TEN)
                .createTime(new Date())
                .updateTime(new Date())
                .build();
        PriceLogEvent priceLogEvent = new PriceLogEvent(this, order);
        applicationContext.publishEvent(priceLogEvent);
        return order;
    }

}
